/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmCamiones.java
 *
 * Created on 11-oct-2011, 9:37:45
 */
package apiformularios.form;

import api.dom.Camion;
import api.dom.Utilitaria;
import api.gest.GestCamiones;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Luis
 */
public class FrmCamiones extends javax.swing.JInternalFrame {

    /** Creates new form FrmCamiones */
    public FrmCamiones() {
        initComponents();
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
        Utilitaria.listarCamiones((DefaultTableModel) jTableCamiones.getModel());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtmatricula = new javax.swing.JTextField();
        txtmarca = new javax.swing.JTextField();
        txtcolor = new javax.swing.JTextField();
        txtejes = new javax.swing.JTextField();
        txtmetros = new javax.swing.JTextField();
        btnAgregar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnatras = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableCamiones = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        btnAgregar1 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel21 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setTitle("Camiones");
        setToolTipText("");
        setMaximumSize(new java.awt.Dimension(580, 570));
        setMinimumSize(new java.awt.Dimension(580, 570));
        setNextFocusableComponent(txtmatricula);
        setNormalBounds(new java.awt.Rectangle(0, 0, 650, 570));
        setPreferredSize(new java.awt.Dimension(650, 570));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel1.setText("Matricula :");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, -1, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Marca :");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, -1, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Color :");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, -1, -1));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("Ejes :");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 80, -1, -1));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel5.setText("Metros :");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 110, -1, -1));

        txtmatricula.setNextFocusableComponent(txtmarca);
        getContentPane().add(txtmatricula, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 80, 160, -1));

        txtmarca.setNextFocusableComponent(txtcolor);
        getContentPane().add(txtmarca, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 110, 200, -1));

        txtcolor.setNextFocusableComponent(txtejes);
        getContentPane().add(txtcolor, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 140, 200, -1));

        txtejes.setNextFocusableComponent(txtmetros);
        txtejes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtejesKeyTyped(evt);
            }
        });
        getContentPane().add(txtejes, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 80, 210, -1));

        txtmetros.setNextFocusableComponent(btnAgregar1);
        txtmetros.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtmetrosKeyTyped(evt);
            }
        });
        getContentPane().add(txtmetros, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 110, 210, -1));

        btnAgregar.setText("Modifcar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });
        getContentPane().add(btnAgregar, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 520, 80, 40));

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        getContentPane().add(btnEliminar, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 520, 80, 40));

        btnatras.setText("Cerrar");
        btnatras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnatrasActionPerformed(evt);
            }
        });
        getContentPane().add(btnatras, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 520, 80, 40));

        jTableCamiones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Matricula", "Marca", "Color", "Ejes", "Metros"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableCamiones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableCamionesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTableCamiones);

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 170, 630, 340));

        jPanel1.setBackground(new java.awt.Color(153, 180, 209));
        jPanel1.setPreferredSize(new java.awt.Dimension(580, 71));

        jLabel6.setBackground(new java.awt.Color(51, 255, 51));
        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 48));
        jLabel6.setText("Camiones");
        jLabel6.setToolTipText("");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addContainerGap(436, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 650, 70));

        btnAgregar1.setText("Agregar");
        btnAgregar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregar1ActionPerformed(evt);
            }
        });
        btnAgregar1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnAgregar1KeyPressed(evt);
            }
        });
        getContentPane().add(btnAgregar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 520, 80, 40));

        jButton1.setText("...");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 80, 30, 20));

        jLabel21.setFont(new java.awt.Font("Tahoma", 2, 12));
        jLabel21.setText("Campos Obligatorios (*)");
        getContentPane().add(jLabel21, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 150, 140, -1));

        jLabel23.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel23.setText("*");
        getContentPane().add(jLabel23, new org.netbeans.lib.awtextra.AbsoluteConstraints(630, 110, 10, -1));

        jLabel24.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel24.setText("*");
        getContentPane().add(jLabel24, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 110, 10, -1));

        jLabel25.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel25.setText("*");
        getContentPane().add(jLabel25, new org.netbeans.lib.awtextra.AbsoluteConstraints(630, 80, 10, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnatrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnatrasActionPerformed
// TODO add your handling code here:         this.dispose();     }//GEN-LAST:event_btnatrasActionPerformed
    }
        private void btnAgregar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregar1ActionPerformed
            // TODO add your handling code here:
            try {
                if ("".equals(txtmatricula.getText()) || "".equals(txtmarca.getText()) || "".equals(txtejes.getText()) || "".equals(txtmetros.getText())) {
                    JOptionPane.showMessageDialog(null, "Falto ingresar algun dato obligatorio.");
                } else {
                    DefaultTableModel modelo = (DefaultTableModel) jTableCamiones.getModel();
                    for (int i = 0; i < modelo.getRowCount();) {
                        modelo.removeRow(i);
                    }
                    if("".equals(txtcolor.getText())){
                        txtcolor.setText("No ingreso");
                    }
                    Camion objC = new Camion(txtmatricula.getText(), txtmarca.getText(), txtcolor.getText(), Integer.parseInt(txtejes.getText()), Double.parseDouble(txtmetros.getText()));
                    GestCamiones.getInstance().altaCamiones(objC);
                    //this.txtNumero.setText(String.valueOf(GestEmpleado.getInstance().devolverUltimoNum()));
                    for (Camion objcam : GestCamiones.getInstance().devolverCamiones()) {
                        Object[] objOp = {objcam.getcMatricula(), objcam.getcMarca(), objcam.getcColor(), objcam.getcEjes(), objcam.getcMetros()};
                        modelo.addRow(objOp);
                    }
                }
                this.txtmatricula.setText("");
                this.txtmarca.setText("");
                this.txtcolor.setText("");
                this.txtejes.setText("");
                this.txtmetros.setText("");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Hubo un error interno mientras se agregaba el camion. \nSi el problema persiste comuniquese a W.O.R.A inc");
            }
        }//GEN-LAST:event_btnAgregar1ActionPerformed

        private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
            // TODO add your handling code here:
            if (jTableCamiones.getSelectedRow() < 0) {
                JOptionPane.showMessageDialog(null, "Seleccione un camoin");
            } else {
                String x = (String.valueOf(this.jTableCamiones.getValueAt(jTableCamiones.getSelectedRow(), 0)));
                /*if (AppAdministracion.getInstance().buscarSiLocalTieneOperador(numero) == false) {
                JOptionPane.showMessageDialog(null, "ALGUN LOCAL ESTA ASOCIADO AL OPERADOR,\n IMPOSIBLE DE ELIMINAR", "Fatal Error", JOptionPane.ERROR_MESSAGE);
                } else {*/
                if ((GestCamiones.getInstance().bajaCamiones(x)) == true) {
                    DefaultTableModel modelo = (DefaultTableModel) jTableCamiones.getModel();
                    for (int i = 0; i < modelo.getRowCount();) {
                        modelo.removeRow(i);
                    }
                    Utilitaria.listarCamiones((DefaultTableModel) jTableCamiones.getModel());
                    txtmatricula.setText("");
                    txtmarca.setText("");
                    txtcolor.setText("");
                    txtejes.setText("");
                    txtmetros.setText("");
                } else {
                    JOptionPane.showMessageDialog(jTableCamiones, "No se puede eliminar el camion. \n Esta en algun registro de facturas o gastos");
                }


            }
        }//GEN-LAST:event_btnEliminarActionPerformed

        private void jTableCamionesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableCamionesMouseClicked
            // TODO add your handling code here:
            txtmatricula.setText(String.valueOf(jTableCamiones.getValueAt(jTableCamiones.getSelectedRow(), 0)));
            txtmarca.setText(String.valueOf(jTableCamiones.getValueAt(jTableCamiones.getSelectedRow(), 1)));
            txtcolor.setText(String.valueOf(jTableCamiones.getValueAt(jTableCamiones.getSelectedRow(), 2)));
            txtejes.setText(String.valueOf(jTableCamiones.getValueAt(jTableCamiones.getSelectedRow(), 3)));
            txtmetros.setText(String.valueOf(jTableCamiones.getValueAt(jTableCamiones.getSelectedRow(), 4)));
        }//GEN-LAST:event_jTableCamionesMouseClicked

        private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
            // TODO add your handling code here:
            if ("".equals(txtmatricula.getText()) || "".equals(txtmarca.getText()) || "".equals(txtejes.getText()) || "".equals(txtmetros.getText()) || "".equals(txtcolor.getText())) {
                JOptionPane.showMessageDialog(null, "Error al modificar el Camion. \n Falto ingresar algun dato.");
            } else {
                Camion objCamion = new Camion(txtmatricula.getText(), txtmarca.getText(), txtcolor.getText(), Integer.parseInt(txtejes.getText()), Double.parseDouble(txtmetros.getText()));
                GestCamiones.getInstance().modificarCamiones(objCamion);
                txtmatricula.setText("");
                txtmarca.setText("");
                txtcolor.setText("");
                txtejes.setText("");
                txtmetros.setText("");
                Utilitaria.listarCamiones((DefaultTableModel) jTableCamiones.getModel());
            }
        }//GEN-LAST:event_btnAgregarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.txtcolor.setText("");
        this.txtejes.setText("");
        this.txtmarca.setText("");
        this.txtmatricula.setText("");
        this.txtmetros.setText("");

    }
        // TODO add your handling code here:         this.txtNumero.setText(String.valueOf(GestEmpleado.getInstance().devolverUltimoNum()));         this.txtNombre1.setText("");         this.txtCedula.setText("");         this.txtDireccion.setText("");         this.txtTelefono.setText("");     }//GEN-LAST:event_jButton1ActionPerformed

    private void txtejesKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtejesKeyTyped
        // TODO add your handling code here:
        char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }
    }//GEN-LAST:event_txtejesKeyTyped

    private void txtmetrosKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtmetrosKeyTyped
        // TODO add your handling code here:
        char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }
    }//GEN-LAST:event_txtmetrosKeyTyped

    private void btnAgregar1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnAgregar1KeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
              btnAgregar1ActionPerformed(null);              
          }
    }//GEN-LAST:event_btnAgregar1KeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnAgregar1;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnatras;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableCamiones;
    private javax.swing.JTextField txtcolor;
    private javax.swing.JTextField txtejes;
    private javax.swing.JTextField txtmarca;
    private javax.swing.JTextField txtmatricula;
    private javax.swing.JTextField txtmetros;
    // End of variables declaration//GEN-END:variables
}
